package com.gzyj.jtt808.common;

import cn.hutool.core.collection.CollectionUtil;
import com.gzyj.jtt808.utils.IntUtils;
import com.gzyj.jtt808.v2011.additionalinfo.JTT808_0x0200_0x01;
import com.gzyj.jtt808.v2011.additionalinfo.JTT808_0x0200_0x02;
import com.gzyj.jtt808.v2011.additionalinfo.JTT808_0x0200_0x6A;
import com.gzyj.jtt808.v2011.additionalinfo.JTT808_AddtionalInfo;
import com.gzyj.jtt808.v2013.model.position.additionalInfo.JTT808_0x0200_0x25;
import com.gzyj.jtt808.v2013.model.position.additionalInfo.JTT808_0x0200_0x30;
import com.gzyj.jtt808.v2013.model.position.additionalInfo.JTT808_0x0200_0x31;

import io.swagger.annotations.ApiModel;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author lhj
 */
@Data
@ApiModel("通用位置定位")
public class GPSPosition implements Serializable {

    public String terminalPhoneNo;

    public String version;

    public Integer msgId;

    public Integer alarmFlagVal;
    public Integer STATUSBIT;
    public Long LAT;//纬度
    public Long LNG;//经度
    public Integer ALTITUDE;//海拔
    public Integer SPEED;//速度
    public Integer DIRECTION;//方向
    public String TIME;//时间
    public List<JTT808_AddtionalInfo> gnssDataAdditionalInfo;//位置附加项列表

    private String jtt808_0x64_platformId;
    private String jtt808_0x65_platformId;
    private String jtt808_0x67_platformId;
    private String jtt808_0x70_platformId;


    public Double getLngExt() {
        return new BigDecimal((double) this.getLNG() / 1000000).setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public Double getLatExt() {
        return new BigDecimal((double) this.getLAT() / 1000000).setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public Date getGPSTimeExt() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.parse(this.getTIME());
        } catch (Exception e) {
            System.out.println("时间报错：" + this.getTIME());
            throw new RuntimeException(e);
        }
    }

    public Integer getSpeedExt() {
        return this.getSPEED() / 10;
    }

    public int Limited6AExt() {
        try {

            if (CollectionUtil.isNotEmpty(this.gnssDataAdditionalInfo)) {

                for (int i = 0; i < this.getGnssDataAdditionalInfo().size(); i++) {
                    JTT808_AddtionalInfo addtionalInfo = this.getGnssDataAdditionalInfo().get(i);
                    if (addtionalInfo.getID() == 0x6A) {
                        JTT808_0x0200_0x6A jtt808_0x0200_0x6A = (JTT808_0x0200_0x6A) addtionalInfo;
                        return jtt808_0x0200_0x6A.getLimited();
                    }

                }

            }

            return 0;
        } catch (Exception ex) {
            return 0;
        }
    }

    public String getAlarmNoteExt() {
        StringBuffer result = new StringBuffer();
        String binary = new StringBuffer(IntUtils.intToBinary(this.getAlarmFlagVal(), 32)).reverse().toString();

        if (binary.charAt(0) == '1') {
            result.append("紧急报警").append(",");
        }
        if (binary.charAt(1) == '1') {
            result.append("超速报警").append(",");
        }

        if (binary.charAt(2) == '1') {
            result.append("疲劳驾驶").append(",");
        }

        if (binary.charAt(3) == '1') {
            result.append("危险预警").append(",");
        }
        if (binary.charAt(4) == '1') {
            result.append("GNSS模块发生故障").append(",");
        }

        if (binary.charAt(5) == '1') {
            result.append("GNSS天线未接或被剪断").append(",");
        }
        if (binary.charAt(6) == '1') {
            result.append("GNSS天线短路").append(",");
        }

        if (binary.charAt(7) == '1') {
            result.append("终端主电源欠压").append(",");
        }
        if (binary.charAt(8) == '1') {
            result.append("终端主电源掉电").append(",");
        }

        if (binary.charAt(9) == '1') {
            result.append("终端LCD或显示器故障").append(",");
        }

        if (binary.charAt(10) == '1') {
            result.append("TTS模块故障").append(",");
        }

        if (binary.charAt(11) == '1') {
            result.append("摄像头故障").append(",");
        }

        if (this.getVersion() != null && this.getVersion().length() > 0 && !this.getVersion().equalsIgnoreCase("2011")) {
            if (binary.charAt(12) == '1') {
                result.append("道路运输证IC卡模块故障").append(",");
            }

            if (binary.charAt(13) == '1') {
                result.append("超速预警").append(",");
            }

            if (binary.charAt(14) == '1') {
                result.append("疲劳驾驶预警").append(",");
            }

            if (binary.charAt(15) == '1') {
                result.append("违规行驶报警").append(",");
            }

            if (binary.charAt(16) == '1') {
                result.append("胎压异常报警").append(",");
            }

            if (binary.charAt(17) == '1') {
                result.append("车道偏离报警").append(",");
            }
        }


        if (binary.charAt(18) == '1') {
            result.append("当天累计驾驶超时").append(",");
        }

        if (binary.charAt(19) == '1') {
            result.append("超时停车").append(",");
        }

        if (binary.charAt(20) == '1') {
            result.append("进出区域").append(",");
        }

        if (binary.charAt(21) == '1') {
            result.append("进出路线").append(",");
        }

        if (binary.charAt(22) == '1') {
            result.append("路段行驶时间不足/过长").append(",");
        }

        if (binary.charAt(23) == '1') {
            result.append("路线偏离报警").append(",");
        }

        if (binary.charAt(24) == '1') {
            result.append("车辆VSS故障").append(",");
        }

        if (binary.charAt(25) == '1') {
            result.append("车辆油量异常").append(",");
        }

        if (binary.charAt(26) == '1') {
            result.append("车辆被盗").append(",");
        }

        if (binary.charAt(27) == '1') {
            result.append("车辆非法点火").append(",");
        }

        if (binary.charAt(28) == '1') {
            result.append("车辆非法位移").append(",");
        }


        if (this.getVersion() != null && this.getVersion().length() > 0 && !this.getVersion().equalsIgnoreCase("2011")) {
            if (binary.charAt(29) == '1') {
                result.append("碰撞预警").append(",");
            }

            if (binary.charAt(30) == '1') {
                result.append("侧翻预警").append(",");
            }

            if (binary.charAt(31) == '1') {
                result.append("非法开门报警").append(",");
            }
        }


        if (result.length() > 0) {
            result.deleteCharAt(result.lastIndexOf(","));
        }

        return result.toString();
    }

    public int MileageExt() {
        try {

            if (CollectionUtil.isNotEmpty(this.gnssDataAdditionalInfo)) {

                for (int i = 0; i < this.getGnssDataAdditionalInfo().size(); i++) {
                    JTT808_AddtionalInfo addtionalInfo = this.getGnssDataAdditionalInfo().get(i);
                    if (addtionalInfo.getID() == 0x01) {
                        JTT808_0x0200_0x01 jtt808_0x0200_0x01 = (JTT808_0x0200_0x01) addtionalInfo;
                        return jtt808_0x0200_0x01.getMileage() * 100;
                    }

                }

            }


            return -1;
        } catch (Exception ex) {
            return -1;
        }
    }

    public int OilExt() {
        try {

            if (CollectionUtil.isNotEmpty(this.gnssDataAdditionalInfo)) {

                for (int i = 0; i < this.getGnssDataAdditionalInfo().size(); i++) {
                    JTT808_AddtionalInfo addtionalInfo = this.getGnssDataAdditionalInfo().get(i);
                    if (addtionalInfo.getID() == 0x02) {
                        JTT808_0x0200_0x02 jtt808_0x0200_0x02 = (JTT808_0x0200_0x02) addtionalInfo;
                        return jtt808_0x0200_0x02.getOil();
                    }
                }
            }

            return 0;
        } catch (Exception ex) {
            return 0;
        }
    }

    public int SignalExt() {
        try {

            if (CollectionUtil.isNotEmpty(this.gnssDataAdditionalInfo)) {

                for (int i = 0; i < this.getGnssDataAdditionalInfo().size(); i++) {
                    JTT808_AddtionalInfo addtionalInfo = this.getGnssDataAdditionalInfo().get(i);
                    if (addtionalInfo.getID() == 0x30) {
                        JTT808_0x0200_0x30 jtt808_0x0200_0x30 = (JTT808_0x0200_0x30) addtionalInfo;
                        return jtt808_0x0200_0x30.getWiFiSignalStrength();
                    }

                }

            }


            return 0;
        } catch (Exception ex) {
            return 0;
        }
    }

    public int SatelliteLenExt() {
        try {

            if (CollectionUtil.isNotEmpty(this.gnssDataAdditionalInfo)) {

                for (int i = 0; i < this.getGnssDataAdditionalInfo().size(); i++) {
                    JTT808_AddtionalInfo addtionalInfo = this.getGnssDataAdditionalInfo().get(i);
                    if (addtionalInfo.getID() == 0x31) {
                        JTT808_0x0200_0x31 jtt808_0x0200_0x31 = (JTT808_0x0200_0x31) addtionalInfo;
                        return jtt808_0x0200_0x31.getGNSSCount();
                    }

                }

            }


            return 0;
        } catch (Exception ex) {
            return 0;
        }
    }

    /**
     * 获取信号灯
     *
     * @return
     */
    public String getSignalExt() {
        try {

            if (CollectionUtil.isNotEmpty(this.gnssDataAdditionalInfo)) {

                for (int i = 0; i < this.getGnssDataAdditionalInfo().size(); i++) {
                    JTT808_AddtionalInfo addtionalInfo = this.getGnssDataAdditionalInfo().get(i);
                    if (addtionalInfo.getID() == 0x25) {
                        JTT808_0x0200_0x25 jtt808_0x0200_0x25 = (JTT808_0x0200_0x25) addtionalInfo;

                        StringBuffer result = new StringBuffer();
                        String binary = new StringBuffer(IntUtils.intToBinary(jtt808_0x0200_0x25.getCarSignalStatus(), 32)).reverse().toString();

                        if (binary.charAt(0) == '1') {
                            result.append("近光灯信号").append(",");
                        }
                        if (binary.charAt(1) == '1') {
                            result.append("远光灯信号").append(",");
                        }

                        if (binary.charAt(2) == '1') {
                            result.append("右转向灯信号").append(",");
                        }

                        if (binary.charAt(3) == '1') {
                            result.append("左转向灯信号").append(",");
                        }
                        if (binary.charAt(4) == '1') {
                            result.append("制动信号").append(",");
                        }

                        if (binary.charAt(5) == '1') {
                            result.append("倒档信号").append(",");
                        }
                        if (binary.charAt(6) == '1') {
                            result.append("雾灯信号").append(",");
                        }

                        if (binary.charAt(7) == '1') {
                            result.append("示廓灯").append(",");
                        }
                        if (binary.charAt(8) == '1') {
                            result.append("喇叭信号").append(",");
                        }

                        if (binary.charAt(9) == '1') {
                            result.append("空调状态").append(",");
                        }

                        if (binary.charAt(10) == '1') {
                            result.append("空挡信号").append(",");
                        }

                        if (binary.charAt(11) == '1') {
                            result.append("缓速器工作").append(",");
                        }

                        if (binary.charAt(12) == '1') {
                            result.append("ABS工作").append(",");
                        }

                        if (binary.charAt(13) == '1') {
                            result.append("加热器工作").append(",");
                        }

                        if (binary.charAt(14) == '1') {
                            result.append("离合器状态").append(",");
                        }


                        if (result.length() > 0) {
                            result.deleteCharAt(result.lastIndexOf(","));
                        }

                        return result.toString();
                    }

                }

            }


            return null;
        } catch (Exception ex) {
            return null;
        }
    }


}
